load("@aspect_rules_esbuild//esbuild:defs.bzl", "esbuild")
load("@npm//:defs.bzl", "npm_link_all_packages")
load("//tools:defaults.bzl", "api_golden_test", "extract_types", "npm_package", "ts_config", "ts_project")

package(default_visibility = ["//visibility:public"])

PUBLIC_TARGETS = [
    ":compiler-cli",
    "//packages/compiler-cli/private:private",
    "//packages/compiler-cli/linker:linker",
    "//packages/compiler-cli/linker/babel:babel",
]

npm_link_all_packages()

ts_config(
    name = "tsconfig_build",
    src = "tsconfig.json",
    deps = [
        "//:node_modules/@types/node",
        "//packages:tsconfig_build",
    ],
)

ts_config(
    name = "tsconfig_test",
    testonly = True,
    src = "tsconfig-test.json",
    deps = [
        ":tsconfig_build",
        "//:node_modules/@types/jasmine",
    ],
)

esbuild(
    name = "bundles",
    config = "esbuild.config.js",
    entry_points = [
        ":index.ts",
        "//packages/compiler-cli:src/bin/ngc.ts",
        "//packages/compiler-cli:src/bin/ng_xi18n.ts",
        "//packages/compiler-cli/linker:files",
        "//packages/compiler-cli/linker/babel:files",
        "//packages/compiler-cli/private:files",
    ],
    external = [
        "@angular/compiler",
        "typescript",
        "@babel/core",
        "reflect-metadata",
        "chokidar",
        "convert-source-map",
        "semver",
        "@jridgewell/sourcemap-codec",
        "tslib",
        "yargs",
    ],
    format = "esm",
    metafile = False,
    platform = "node",
    splitting = True,
    target = "node20",
    deps = PUBLIC_TARGETS,
)

ts_project(
    name = "compiler-cli",
    srcs = glob(
        [
            "*.ts",
            "src/**/*.ts",
        ],
        exclude = [
            "src/integrationtest/**/*.ts",
        ],
    ),
    deps = [
        "//:node_modules/@types/yargs",
        "//:node_modules/chokidar",
        "//:node_modules/reflect-metadata",
        "//:node_modules/typescript",
        "//packages/compiler",
        "//packages/compiler-cli/private",
        "//packages/compiler-cli/src/ngtsc/core",
        "//packages/compiler-cli/src/ngtsc/core:api",
        "//packages/compiler-cli/src/ngtsc/diagnostics",
        "//packages/compiler-cli/src/ngtsc/docs",
        "//packages/compiler-cli/src/ngtsc/file_system",
        "//packages/compiler-cli/src/ngtsc/incremental",
        "//packages/compiler-cli/src/ngtsc/indexer",
        "//packages/compiler-cli/src/ngtsc/logging",
        "//packages/compiler-cli/src/ngtsc/perf",
        "//packages/compiler-cli/src/ngtsc/program_driver",
        "//packages/compiler-cli/src/ngtsc/reflection",
        "//packages/compiler-cli/src/ngtsc/shims",
        "//packages/compiler-cli/src/ngtsc/transform/jit",
        "//packages/compiler-cli/src/ngtsc/translator",
        "//packages/compiler-cli/src/ngtsc/typecheck",
        "//packages/compiler-cli/src/ngtsc/typecheck/api",
        "//packages/compiler-cli/src/ngtsc/util",
    ],
)

extract_types(
    name = "api_type_definitions",
    deps = PUBLIC_TARGETS,
)

npm_package(
    srcs = [
        "package.json",
        ":api_type_definitions",
        ":bundles",
    ],
    package = "@angular/compiler-cli",
    tags = [
        "release-with-framework",
    ],
    visibility = [
        "//adev:__pkg__",
        "//integration:__subpackages__",
        "//modules/benchmarks:__subpackages__",
        "//modules/playground:__subpackages__",
        "//modules/ssr-benchmarks:__subpackages__",
        "//packages/compiler-cli/integrationtest:__pkg__",
        "//packages/core/test/bundling:__subpackages__",
        "//packages/core/test/playground/zone-signal-input:__pkg__",
    ],
)

api_golden_test(
    name = "error_code_api",
    data = [
        ":compiler-cli",
        "//goldens:public-api",
    ],
    entry_point = "src/ngtsc/diagnostics/src/error_code.d.ts",
    golden = "goldens/public-api/compiler-cli/error_code.api.md",
)

api_golden_test(
    name = "extended_template_diagnostic_name_api",
    data = [
        ":compiler-cli",
        "//goldens:public-api",
    ],
    entry_point = "src/ngtsc/diagnostics/src/extended_template_diagnostic_name.d.ts",
    golden = "goldens/public-api/compiler-cli/extended_template_diagnostic_name.api.md",
)

api_golden_test(
    name = "compiler_options_api",
    data = [
        ":compiler-cli",
        "//goldens:public-api",
    ],
    entry_point = "src/ngtsc/core/api/src/public_options.d.ts",
    golden = "goldens/public-api/compiler-cli/compiler_options.api.md",
)
